package it.aekidna.cleverplatypus.logging
{
	import flash.events.StatusEvent;
	import flash.net.LocalConnection;
	import flash.utils.getQualifiedClassName;
	
	import mx.core.Application;
	import mx.logging.AbstractTarget;
	import mx.logging.LogEvent;
	import mx.logging.LogLogger;
	import mx.logging.targets.TraceTarget;

	public class LocalConnectionLogTarget extends AbstractTarget
	{
		public var destination : String = "";
		
		private var _localConnection : LocalConnection;

		public function LocalConnectionLogTarget()
		{
			super();
			_localConnection = new LocalConnection();
			_localConnection.allowDomain("*");
			_localConnection.allowInsecureDomain("*");
			_localConnection.addEventListener( StatusEvent.STATUS, onStatus );
		}

		override public function logEvent( inEvent : LogEvent ) : void
		{
			if( inEvent.level < level )
				return;
			var info : LogInfo = new LogInfo();
			info.level = inEvent.level;
			info.message = inEvent.message;
			info.domain = getQualifiedClassName( Application.application ).replace( /.*::/g, "" );
			info.category = LogLogger( inEvent.target ).category;
			_localConnection.send( "app#" + destination + ":reader", "log", info );
		}
		
		private function onStatus( inEvent : StatusEvent ) : void
		{
			
		}
	}
}